<?php
/**
 * Product's management classs
 *
 * @access		public
 * @author		Svilen Spasov <svilen@svilen.com>
 * @package		AMBilling System
 *
 * @since			28-02-2006
 * @version		1.9
 */
class Product {
	
	/**
   * error/debug object
	 *
	 * @access	public
	 * @var			object
	 */
	var $error;
	
	/**
	 * anyDB handler
	 *
	 * @access	private
	 * @var			object
	 */
	var $anydb;
	
	/**
	 * Min price object
	 *
	 * @access	public
	 * @var			object
	 */
	var $minPrice;
	
	/**
	 * Max price object
	 *
	 * @access	public
	 * @var			object
	 */
	var $maxPrice;
	
	/**
	 * Wholesale price object
	 *
	 * @access	public
	 * @var			object
	 */
	var $wholesale;
	
	/**
	 * Retail price object
	 *
	 * @access	public
	 * @var			object
	 */
	var $retail;
	
	/**
	 * Product Id
	 *
	 * @access	public
	 * @var			int
	 */
	var $productId;
	
	/**
	 * Reseller Id
	 *
	 * @access	public
	 * @var			int
	 */
	var $resellerId;
	
	/**
	 * Store Id
	 *
	 * @access	public
	 * @var			int
	 */
	var $storeId;
	
	/**
	 * Product name
	 *
	 * @access	public
	 * @var			string
	 */
	var $name;
	
	/**
	 * Product type
	 *
	 * @access	public
	 * @var			int
	 */
	var $type;
	
	/**
	 * Product services
	 *
	 * @access	public
	 * @var			array
	 */
	var $services;
	
	/**
	 * Product count
	 *
	 * @access	public
	 * @var			int
	 */
	var $count;
	
	/**
	 * Product life
	 *
	 * @access	public
	 * @var			int
	 */
	var $life;
	
	/**
	 * Product seq
	 *
	 * @access	public
	 * @var			int
	 */
	var $seq;
	
	/**
	 * Product state
	 *
	 * @access	public
	 * @var			int
	 */
	var $state;
	
	/**
	 * Product's rp_product_id
	 *
	 * @access	public
	 * @var			int
	 */
	var $rp_product_id;
	
	/**
	 * Product offer_product
	 *
	 * @access	public
	 * @var			bool
	 */
	var $offer_product;
	
	/**
	 * Product plan_id
	 *
	 * @access	public
	 * @var			string
	 */
	var $plan_id;
	
	/**
	 * Products table
	 *
	 * @access	public
	 * @var			string
	 */
	var $productsTable;
	
	/**
	 * Product id field
	 *
	 * @access	private
	 * @var			string
	 */
	var $productIdField;
	
	/**
		* Constructor initializes the product
		*
		* @access   public
		* @author   Svilen <svilen@svilen.com>
		*
		* @see 			Error::addError
		*
		* @since    24-02-2006
		* @version  1.0
		*
		*/
	function Product ($dbHandler, $error, $productId, $period = 12, $currency = 'USD', $productsTable = 'products', $productIdField = 'product_id') {
		// Initialize error handler
		$this->error = $error;
		
		if (empty($dbHandler)) {
			$this->error->addError(1, 'Product requires anydb handler to be passed', FATAL_ERROR, TRUE);
		}
		
		if (empty($productsTable)) {
			$this->error->addError(2, 'Product requires correct db table to be passed', FATAL_ERROR, TRUE);
		}
		
		// add db handler
		$this->anydb = $dbHandler;
		$this->productId = $productId;
		$this->productsTable = $productsTable;
		$this->productIdField = $productIdField;
		
		// get product details
		$this->getProductDetails();
		
		// create price objects
		$this->minPrice = new Price (&$this->anydb, &$this->error, $this->productId, 'min_price', $period, $currency, $this->productIdField);
		$this->maxPrice = new Price (&$this->anydb, &$this->error, $this->productId, 'max_price', $period, $currency, $this->productIdField);
		$this->wholesale = new Price (&$this->anydb, &$this->error, $this->productId, 'wholesale', $period, $currency, $this->productIdField);
		$this->retail = new Price (&$this->anydb, &$this->error, $this->productId, 'price', $period, $currency, $this->productIdField);
	}
	
	/**
		* Get product details
		*
		* @access   public
		* @author   Svilen <svilen@svilen.com>
		*
		* @see			AnyDB::query()
		* @see			AnyDB::getNext()
		* @see 			Error::addError
		*
		* @since    02-03-2006
		* @version  1.0
		*
		*/
	function getProductDetails () {
		$productId = (int) $this->productId;
				
		if (empty($productId)) {
			$this->error->addError(4, 'Wrong params for getProductDetails() method in Product class');
			return FALSE;
		}
		
		if ($this->productIdField == 'products') {
			$resellerIdSelect = 'reseller_id as reseller_id, store_id as store_id, count as count,';
		}
		
		$query = <<<echo
SELECT $this->productIdField as product_id, $resellerIdSelect name as name, type as type, services as services, life as life, seq as seq, state as state, rp_product_id as rp_product_id, offer_product as offer_product, plan_id as plan_id FROM $this->productsTable WHERE $this->productIdField = $productId LIMIT 1
echo;
		$this->anydb->query($query);
		
		// check for error
		if (!empty($this->anydb->error)) {
			$this->error->addError(3, $this->anydb->error);
			return FALSE;
		}
		
		// get values
		$recordSet = $this->anydb->getNext();
		$this->productId = $recordSet['product_id'];
		$this->resellerId = !empty($recordSet['reseller_id']) ? $recordSet['reseller_id'] : NULL;
		$this->storeId = !empty($recordSet['store_id']) ? $recordSet['store_id'] : NULL;
		$this->name = $recordSet['name'];
		$this->type = $recordSet['type'];
		$this->services = decodeDbStrings($recordSet['services']);
		$this->count = $recordSet['count'];
		$this->life = $recordSet['life'];
		$this->seq = $recordSet['seq'];
		$this->state = $recordSet['state'];
		$this->rp_product_id = $recordSet['rp_product_id'];
		$this->offer_product = $recordSet['offer_product'];
		$this->plan_id = $recordSet['plan_id'];
	}
	
	/**
		* Sets detauls currency to all price objects
		*
		* @access   public
		* @author   Svilen <svilen@svilen.com>
		*
		* @param		string		$currency			Currency string
		*
		* @return		bool			TRUE/FALSE
		*
		* @since    01-03-2006
		* @version  1.0
		*
		*/
	function setPriceCurrency ($currency = 'USD') {
		if (empty($currency)) {
			return FALSE;	
		}
		
		// sets default currency for all prices
		$this->minPrice->currency = $currency;
		$this->maxPrice->currency = $currency;
		$this->wholesale->currency = $currency;
		$this->retail->currency = $currency;
		
		return TRUE;
	}
	
	/**
		* Sets detauls period to all price objects
		*
		* @access   public
		* @author   Svilen <svilen@svilen.com>
		*
		* @param		int				$period			Period value
		*
		* @return		bool			TRUE/FALSE
		*
		* @since    01-03-2006
		* @version  1.0
		*
		*/
	function setPricePeriod ($period = 12) {
		if (empty($period)) {
			return FALSE;	
		}
		
		// sets default period for all prices
		$this->minPrice->period = $period;
		$this->maxPrice->period = $period;
		$this->wholesale->period = $period;
		$this->retail->period = $period;
		
		return TRUE;
	}
	
	/**
	 * Get price by type, period
	 *
	 * @access   public
	 * @author   Svilen Spasov <svilen@svilen.com>
	 *
	 * @param 	 string		$priceType			price type
	 * @param 	 int			$period					period
	 *
	 * @return 	 float		price
	 *
	 * @see 		 Error::addError()
	 *
	 * @since    10-03-2006
	 * @version  1.0
	 */
	function getPriceByType ($priceType = 'price', $period = 12, $currency = NULL) {
		$price = '';
		
		if (empty($priceType) || empty($period)) {
			return FALSE;	
		}
		
		switch ($priceType) {
			case 'min_price':
				$price = $this->minPrice->get($period, $currency);
				break;
			case 'max_price':
				$price = $this->maxPrice->get($period, $currency);
				break;
			case 'wholesale':
				$price = $this->wholesale->get($period, $currency);
				break;
			case 'price':
				$price = $this->retail->get($period, $currency);
				break;
		}
		
		return $price;
	}
	
	/**
	 * Get VAT by type, period
	 *
	 * @access   public
	 * @author   Svilen Spasov <svilen@svilen.com>
	 *
	 * @param 	 string		$priceType			price type
	 * @param 	 int			$period					period
	 *
	 * @return 	 float		price
	 *
	 * @see 		 Error::addError()
	 *
	 * @since    10-03-2006
	 * @version  1.0
	 */
	function getVatByType ($priceType = 'price', $period = 12, $currency = NULL) {
		$price = '';
		
		if (empty($priceType) || empty($period)) {
			return FALSE;	
		}
		
		switch ($priceType) {
			case 'min_price':
				$price = $this->minPrice->getVat($period, $currency);
				break;
			case 'max_price':
				$price = $this->maxPrice->getVat($period, $currency);
				break;
			case 'wholesale':
				$price = $this->wholesale->getVat($period, $currency);
				break;
			case 'price':
				$price = $this->retail->getVat($period, $currency);
				break;
		}
		
		return $price;
	}
	
	/**
	 * Get total price by type, period
	 *
	 * @access   public
	 * @author   Svilen Spasov <svilen@svilen.com>
	 *
	 * @param 	 string		$priceType			price type
	 * @param 	 int			$period					period
	 *
	 * @return 	 float		price
	 *
	 * @see 		 Error::addError()
	 *
	 * @since    10-03-2006
	 * @version  1.0
	 */
	function getTotalPriceByType ($priceType = 'price', $period = 12, $currency = NULL) {
		$price = '';
		
		if (empty($priceType) || empty($period)) {
			return FALSE;	
		}
		
		switch ($priceType) {
			case 'min_price':
				$price = $this->minPrice->getTotal($period, $currency);
				break;
			case 'max_price':
				$price = $this->maxPrice->getTotal($period, $currency);
				break;
			case 'wholesale':
				$price = $this->wholesale->getTotal($period, $currency);
				break;
			case 'price':
				$price = $this->retail->getTotal($period, $currency);
				break;
		}
		
		return $price;
	}
}
?>